const api = require("../../utils/cloudAPIUtils")
const app = getApp();

Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canPhone: true,
    msg:'需要获得您的公开信息(昵称，头像等)',
    loginLevel:2,
  },

  /**
   * 手机号注册
   */
  btnPhoneRegister:function(e){
    console.log("=====>",17)
    api.post(api.url.registerPhoneUserInfo,{
      encryptedData:e.detail.encryptedData,
      iv:e.detail.iv,
      sessionKey:app.globalData.userInfo.sessionKey,
      memberId: app.globalData.userInfo.memberId,
      appId:wx.getAccountInfoSync().miniProgram.appId
    },function(res){
      console.log("=====>",25)
      console.log(res.data);
      if(res.data.code == 200){
          app.globalData.userInfo.memberId = res.data.body.memberId;
          app.globalData.userInfo.memberName = res.data.body.memberName;
          app.globalData.userInfo.memberFaceImg = res.data.body.memberFaceImg;
          app.globalData.userInfo.memberPhone = res.data.body.memberPhone;
          app.globalData.userInfo.loginToken = res.data.body.loginToken;
          app.globalData.userInfo.loginLevel = 3;
          wx.navigateBack();
      }else if(res.data.msg){
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: '获取信息失败',
          icon: 'error',
          duration: 2000
        })
      }
    });
  },

  /**
   * 公共信息注册
   */
  btnRegister:function(e){
    var self = this;
    console.log(app.globalData.userInfo);
    if(app.globalData.userInfo){
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (res) => {
          api.post(api.url.registerUserInfo,{
              encryptedData:res.encryptedData,
              iv:res.iv,
              sessionKey:app.globalData.userInfo.sessionKey,
              memberId: app.globalData.userInfo.memberId,
              appId:wx.getAccountInfoSync().miniProgram.appId
          },function(res){
              console.log("=====>",76)
              console.log(res.data);
              if(res.data.code == 200){
                  app.globalData.userInfo.memberId = res.data.body.memberId;
                  app.globalData.userInfo.memberName = res.data.body.memberName;
                  app.globalData.userInfo.memberFaceImg = res.data.body.memberFaceImg;
                  app.globalData.userInfo.memberPhone = res.data.body.memberPhone;
                  app.globalData.userInfo.loginToken = res.data.body.loginToken;
                  if(res.data.body.modules){
                    app.globalData.userInfo.modules =res.data.body.modules
                  }
                  if(app.globalData.userInfo.loginLevel == 3){
                    self.setData({
                      canIUse:false,
                      canPhone:true
                    })
                    return
                  }else{
                    app.globalData.userInfo.loginLevel = 0;
                  }
                  let pages = getCurrentPages();        ///////////////////??
                  pages =pages[pages.length - 2];
                  pages.setData({goBackToAut:true})
                  wx.navigateBack({
                    delta: 1
                  });
              }else if(res.data.msg){
                wx.showToast({
                  title: res.data.msg,
                  icon: 'error',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title: '获取信息失败',
                  icon: 'error',
                  duration: 2000
                })
              }
          });
        },
        fail:function(){
          wx.showToast({
            title: '获取信息失败',
            icon: 'error',
            duration: 2000
          })
        }
      });
    }else{
      wx.showModal({
        title: '提示',
        content: '登陆失败，请联系客服人员',
        showCancel:false,
        success:function(){
          wx.navigateTo({
            url: "../index/index"
          })
        }
      })
    }
  },
})